﻿using System;
using System.Collections.Generic; 
using System.Linq;
using NTTDATA.BEENChina.ResultCertification.PropertyGridUtil;
using NTTDATA.BEENChina.ResultCertification.PropertyGridUtil.CustomObject;
using NTTDATA.BEENChina.ResultCertification.Util;

namespace NTTDATA.BEENChina.ResultCertification.Checker
{
    public class Verfity0301 : VerfityMulitResult
    {
        private CustomCollection<CheckPair> _checkList;

        public Verfity0301(CustomCollection<CheckPair> checkList)
        {            
            this._checkList = checkList;
        }

        public override bool CheckResult(List<List<string>> result1, string entryCommon1, List<List<string>> result2, string entryCommon2)
        {
            ////2个结果中如果有一个带问号 付签结果是20 不比较
            //if (entryCommon1.Equals("20") || entryCommon2.Equals("20"))
            //{
            //    return false;
            //}

            List<string[]> r1= result1.SelectMany(o => o.Select(p => p.Split(new char[] { '/' }))).ToList();
            List<string[]> r2 = result2.SelectMany(o => o.Select(p => p.Split(new char[] { '/' }))).ToList();
              
            for(int i=0;i<r1.Count() && i<r2.Count();i++)
            {
                for (int j = 0; j < r1[i].Count() && j < r2[i].Count(); j++)
                {
                    if (r1[i][j].Contains("?") || r2[i][j].Contains("?"))
                    {
                        continue;
                    }
                    //if( Enumerable.Any<CheckPair>(checkList,o => r1[i][j].Contains(o.Key) && r2[i][j].Contains(o.Value)
                    //                     || r1[i][j].Contains(o.Value) && r2[i][j].Contains(o.Key)))

                    //2个对比结果中如果有？
                    //去掉问号在比较
                   /* if (checkList.Any(o => checkPair(o,
                        r1[i][j].Replace("?",string.Empty)
                        , r2[i][j].Replace("?", string.Empty))))*/
                    //去掉问号有问题 ，恢复
                    if (_checkList.Any(o => CheckPair(o,r1[i][j], r2[i][j])))
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        private bool CheckPair(CheckPair pair, string result1, string result2)
        {
             return CheckString(pair.Key, pair.Value, result1, result2)
                 || CheckString(pair.Value, pair.Key , result1, result2); 

           // return CheckString(pair.Key, pair.Value, result1, result2);
        }

        private bool CheckString(string checkStr1, string checkStr2, string result1, string result2)
        {

            //if (checkStr1.Equals("発熱"))
            //    Console.WriteLine(checkStr1);

           /*

            var array1 = SplitString(result1, checkStr1.Length);
            var array2 = SplitString(result2, checkStr2.Length);

            int arrayCount = Math.Min(array1.Count, array2.Count);

            for (int i = 0; i < arrayCount; i++)
            {
                if (String.CompareOrdinal(checkStr1, array1[i].ToString(CultureInfo.InvariantCulture)) ==0  &&
                    String.CompareOrdinal(checkStr2, array2[i].ToString(CultureInfo.InvariantCulture)) ==0  )
                {

                    var reason = string.Format("{0} / {1}", checkStr1, checkStr2);
                     Console.WriteLine(reason);
                   // ChildImageObject.Reason.Reason = string.Format("{0} / {1}", checkStr1, checkStr2);

                    return true;
                }
            }

            return false;*/



            int starIndex = 0;
            int keyIndex = -1;


            var result = result1;

            do
            {

                keyIndex = result.IndexOf(checkStr1, starIndex, System.StringComparison.CurrentCulture);

                if (keyIndex.Equals(-1))
                    break;

                //if (keyIndex.Equals(0) || keyIndex.Equals(result.Length - 1))
                //    continue;


                if (keyIndex + checkStr1.Length <= result2.Length)
                {
                    var tempVal = result2.Substring(keyIndex, checkStr1.Length);

                    if (String.CompareOrdinal(checkStr2, tempVal) == 0)
                    {
                        //var reason = string.Format("{0} / {1}", checkStr1, checkStr2);
                        //Console.WriteLine(reason);

                        

                        if (ChildImageObject != null)
                        {
                            ChildImageObject.Reason.ErrorRanage =
                            string.Format("{0}：{1}{2}{3}：{4}",
                                           StrHelper.RankToString(CompareRankX), checkStr1, Environment.NewLine,
                                           StrHelper.RankToString(CompareRankY), checkStr2
                                );
                        }
                            
                        return true;
                    }
                }

                starIndex = keyIndex + 1;

            } while (keyIndex < 0 || starIndex > result.Length);


            return false;
        }
        
      /*  private List<string> SplitString(string result,int width)
        {
            int i = 0;
            var results = new List<string>();
            while (i < result.Length)
            {
                var part = string.Empty;

                if (i + width < result.Length)
                {
                    part = result.Substring(i, width);
                }
                else
                {
                    part = result.Substring(i);
                }
                
                results.Add(part);

                i++;
            }

            return results;
        }*/
    }
}